#include "VSVertexColor.h"
#include "VSShaderStringFactory.h"
#include "VSGraphicInclude.h"
#include "VSStream.h"
using namespace VSEngine2;
unsigned int VSVertexColor::ms_uiOutputNodeNum = 0;
IMPLEMENT_RTTI(VSVertexColor,VSShaderFunction)
static bool gs_bStreamRegistered_VSVertexColor = VSVertexColor::RegisterMainFactory();
bool VSVertexColor::ms_bRegisterMainFactory = false;
bool VSVertexColor::RegisterMainFactory()
{
	if (!ms_bRegisterMainFactory)
	{ 
		VSMain::AddInitialFuntion(VSVertexColor::InitialClassFactory); 
		ms_bRegisterMainFactory = true;
	} 
	return ms_bRegisterMainFactory;

}
bool VSVertexColor::InitialClassFactory()
{
	ms_ClassFactory.AddElement(ms_Type.GetName(),FactoryFunc);

	return 1;
}
VSObject * VSVertexColor::FactoryFunc()
{
	return VS_NEW VSVertexColor;

}
VSVertexColor::VSVertexColor(const VSUsedName & ShowName,VSMaterial * pMaterial)
:VSShaderFunction(ShowName,pMaterial)
{


	VSString OutputID = IntToString(ms_uiOutputNodeNum);
	VSString OutputName = _T("VertexColorOutput") + OutputID;
	VSOutputNode * pOutputNode = NULL;
	pOutputNode = VS_NEW VSOutputNode(VSPutNode::VT_4,OutputName,this);
	VSMAC_ASSERT(pOutputNode);
	m_pOutput.AddElement(pOutputNode);
	ms_uiOutputNodeNum++;
	m_uiColorIndex = 0;
}
VSVertexColor::VSVertexColor()
{
	m_uiColorIndex = 0;
}
VSVertexColor::~VSVertexColor()
{

}
bool VSVertexColor::GetFuntionString(VSString &OutString)const
{
	if (VSShaderStringFactory::ms_PSColor[m_uiColorIndex].GetLength() > 0)
	{
		OutString += GetOutputNode(OUT_COLOR)->GetNodeName().GetString() + _T(" = ") + VSShaderStringFactory::ms_PSColor[m_uiColorIndex] + _T(";\n");
	}
	return 1;
}

unsigned int VSVertexColor::DiskUse()const
{
	unsigned int Use = VSShaderFunction::DiskUse();
	Use += sizeof(unsigned int);
	return Use;

}
bool VSVertexColor::Save(VSStream & rStream,unsigned int &iSaveUse)const
{
	if(!VSShaderFunction::Save(rStream,iSaveUse))
		return false;


	if(!rStream.Write(&m_uiColorIndex ,sizeof(unsigned int)))
		return 0;

	return 1;
}
bool VSVertexColor::Load(VSStream & rStream,unsigned int &iSaveUse)
{

	if(!VSShaderFunction::Load(rStream,iSaveUse))
		return false;



	if(!rStream.Read(&m_uiColorIndex ,sizeof(unsigned int)))
		return 0;


	return 1;
}
bool VSVertexColor::Clone(const VSObject *pObject,unsigned int uiType)
{
	//NO USE
	const VSVertexColor* Temp = DynamicCast<VSVertexColor>(pObject); 
	if(!Temp)
		return 0;
	if(VSShaderFunction::Clone(pObject,uiType))
	{
		m_uiColorIndex = Temp->m_uiColorIndex;
		return 1;
	}
	else
	{
		return 0;

	}
}